import { msg } from './util.js';
class Http {
	constructor(arg) {
		
	}
	request(url, data = {}, method){
		let token = uni.getStorageSync('token');
		
		const baseUrl = 'http://172.30.143.46:8081';
		let param = {
			url: baseUrl + url,
			data: data,
			method: method ? 'GET':'POST',
			header: method ? {
				"Accept-Language":uni.getStorageSync('lang'),
				'X-Requested-With': 'XMLHttpRequest',
				"Accept": "application/json",
				"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
				"authorization": "bearer " + token
			} : {
				"Accept-Language":uni.getStorageSync('lang'),
				'X-Requested-With': 'XMLHttpRequest',
				'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
				"authorization": "bearer " + token
			},
			dataType: 'json'
		}
		return new Promise((resolve, reject) => {
			uni.request(param).then((res) => {
				// console.log('用户状态:', res[1].data.code)
				if(res[1].data.code == -40001){
					uni.clearStorageSync();
						uni.redirectTo({
							url: '/pages/components/login/login'
						});
				} else{
					resolve(res[1]['data'])
				}
			}).catch((error) => {
				reject(error)
			})
		})
	}
}




export default new Http()